package cn.clwxy.gdz.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.clwxy.gdz.bo.RegisterNewTeacher;
import cn.clwxy.gdz.dao.StudentDao;
import cn.clwxy.gdz.dao.StudentDaoImpl;
import cn.clwxy.gdz.entity.Student;
import cn.clwxy.gdz.service.RegisterNewTeacherService;
import cn.clwxy.gdz.service.RegisterNewTeacherServiceImpl;
import cn.clwxy.gdz.util.MD5Encrypter;

public class RegisterServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		System.out.println("����ע��servlet");
		
		
//		获取网页数据
		String phone = request.getParameter("phone");
		String pwd = request.getParameter("md5Pwd");
		String type = request.getParameter("type");
		String picCode=request.getParameter("picCode");
		String mobile_code=request.getParameter("mobile_code");
//		验证图片验证码
		HttpSession session=request.getSession();
		String prePicCode=(String)session.getAttribute("pic");
		if(!prePicCode.equals(picCode)){
			response.getWriter().write("0");	
		}
		
//		验证手机验证码
		String preMobile_code=String.valueOf(SmsServlet.mobile_code);
		if(!mobile_code.equals(preMobile_code)){
			response.getWriter().write("0");	
		}
		
		
		
		
		pwd = phone + pwd + phone;
		pwd = MD5Encrypter.encrypt(pwd);
		Integer userType = Integer.valueOf(type);
		if (userType == 1) {
			String studentPwd = pwd;
			String studentPhone = phone;
			Student student = new Student();
			Date studentDate = new Date();
			student.setStudentPhone(studentPhone);
			student.setStudentPwd(studentPwd);
			student.setStudentDate(studentDate);
			StudentDao studentDao = new StudentDaoImpl();
			int resu = studentDao.registerStudent(student);
			System.out.println("ѧ��ע����" + resu);
			if(resu==1){
				response.getWriter().write("1");	
			}
		} else if (userType == 2) {
			String teacherPwd = pwd;
			String teacherPhone = phone;
			Date teacherRegisterDate = new Date();
			Date teacherupdatedate = new Date();
			RegisterNewTeacher registerNewTeacher = new RegisterNewTeacher();
			registerNewTeacher.setTeacherPhone(teacherPhone);
			registerNewTeacher.setTeacherPwd(teacherPwd);
			registerNewTeacher.setTeacherRegisterDate(teacherRegisterDate);
			registerNewTeacher.setTeacherupdatedate(teacherupdatedate);
			RegisterNewTeacherService registerNewTeacherService = new RegisterNewTeacherServiceImpl();
			int result = registerNewTeacherService
					.addRegisterNewTeacher(registerNewTeacher);
			String resu = String.valueOf(result);
			System.out.println("��ʦע����" + resu);
			if(result==1){
				response.getWriter().write("2");	
			}
		}else{
			System.out.println("ע��ʧ��");
			response.getWriter().write("0");
		}

	}
}
